package com.yixing.tech.wcs.tcp;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.tcp.pc.TcpMessage;
import com.yixing.tech.common.tcp.pc.TcpReply;
import com.yixing.tech.wcs.service.ICRUDService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InteractionConsumer {

    // 容器编号：交互状态
    public static final Map<String, String> map = new ConcurrentHashMap<>();

    @Resource
    IInteractionService interactionService;

    @Resource
    ICRUDService crudService;

    public Function<TcpMessage, TcpReply> consumeFunc() {
        return this::consume;
    }

    /** 查询数据库表中待交互的CTU任务 */
    /** 记录当前正在交互的CTU任务的步骤 */
    /** 根据步骤对接收到的不同信息做处理 */

    public TcpReply consume(TcpMessage message) {
        log.info("consume message:{}", message);
        String msg = message.getMessage().replaceAll(" ", "");
        String key = msg.substring(8, 10);
        if("81".equals(key)) {
            interactionService.handleState(msg);
        }else if("82".equals(key)) {    // 分拣台输送线回复已收到 ”请求放下料箱“
            // 不实现
            log.debug("message82 do nothing");
        }else if("83".equals(key)) {    // 分拣台输送线回复已收到 ”完成放下料箱“

            interactionService.finPutDown();
        }else if("84".equals(key)) {    // 分拣台输送线回复已收到 ”完成分拣任务“

            interactionService.finPickingTask();
        }else if("85".equals(key)) {    // 分拣台输送线回复已收到 ”请求取出料箱“
            // 不实现
            log.debug("message85 do nothing");
        }else if("86".equals(key)) {    // 分拣台输送线回复已收到 ”完成取出料箱“

            interactionService.finPickUp();
        }
        return null;
    }

    /**
     * 更新map中容器编号对应的状态
     * @param m 容器编号-状态映射对
     * @param s1 原状态
     * @param s2 目标状态
     * */
    public List<String> updateState(Map<String, String> m, InteractionState s1, InteractionState s2, boolean onlyOne) {
        List<String> podCodeList = new ArrayList<>();

        for(Map.Entry<String, String> entry: m.entrySet()) {
            if(s1.getDictId().equals(entry.getValue())) {
                podCodeList.add(entry.getKey());

                // 更新状态
                entry.setValue(s2.getDictId());

                if(onlyOne)
                    break;
            }
        }
        return podCodeList;
    }

    public Object listPodInteractionState() {
        Map<String, List> res = map.entrySet().stream().collect(Collectors.groupingBy(e -> e.getValue(), Collectors.toCollection(ArrayList::new)));
        return res;
    }

    public void addPodInteractionState(String podCode, String stateName) throws BusinessException {
        if(map.containsKey(podCode))
            throw new BusinessException("容器交互状态已存在!");
        InteractionState interactionState = InteractionState.getByName(stateName);
        if(interactionState == null)
            throw new BusinessException("状态不存在!");

        map.put(podCode, interactionState.getDictId());
    }

    public void removePodInteractionState(String podCode) {
        map.remove(podCode);
    }
}
